//
//  MyRequest.m
//  kankanVideo
//
//  Created by wangbo on 5/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyRequest.h"
#import "Reachability.h"
#import "ASIHTTPRequest.h"
#import "ASIDownloadCache.h"
#import "ASIFormDataRequest.h"

@implementation MyRequest

static MyRequest *myrequest=nil;

-(void)dealloc{
    [super dealloc];
}

+(id)shareInstance{
    if (!myrequest) {
        myrequest=[[MyRequest alloc]init];
    }
    return myrequest;
}

-(BOOL)checkNetwork{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    if (netStatus!=kNotReachable) { // ok
        return YES;
    }else{
        return NO;
    }
}

-(void)loaddataWithURL:(NSString *)weburl forObject:(id)obj{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    if (netStatus!=kNotReachable) { // ok
        reachable = YES;
    }
    weburl=[weburl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSURL *url=[NSURL URLWithString:weburl];
    ASIHTTPRequest *asirequest = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
    asirequest.timeOutSeconds = 30;
    if (reachable){
        [asirequest setCachePolicy:ASIUseDefaultCachePolicy];
    }else{ // use cache if have
        NSLog(@"not reached");
        [asirequest setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
    }
    [asirequest setDownloadCache:[ASIDownloadCache sharedCache]];
    [asirequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
    asirequest.didFinishSelector = @selector(requestFinishedHandler:);
    asirequest.didFailSelector = @selector(requestFailedHandler:);
    asirequest.delegate = obj;
    [asirequest startAsynchronous];
}

-(int)uploadVideoWithVideoURL:(NSURL *)_videoURL forObject:(id)obj{
    NSLog(@"uploadVideoWithVideoURL_start");
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    if (netStatus!=kNotReachable) { // ok
        reachable = YES;
    }else{
        reachable=NO;
    }
    if(reachable){
        NSURL *url=[NSURL URLWithString:@"http://114.80.151.69/oms/video-upload/e/enews/upload3.php"];
        ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
        [request setFile:[_videoURL path] forKey:@"Filedata"];
        [request setDelegate:obj];
        request.didFinishSelector = @selector(requestFinishedHandler:);
        request.didFailSelector = @selector(requestFailedHandler:);
        [request startAsynchronous];
    }
    NSLog(@"uploadVideoWithVideoURL_end");
    return 0;
}


-(int)uploadVideoWithData:(NSMutableArray *)_array forObject:(id)obj{
    NSLog(@"uploadVideoWithData_start");
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    if (netStatus!=kNotReachable) { // ok
        reachable = YES;
    }else{
        reachable=NO;
    }
    if(reachable){
        NSURL *url=[NSURL URLWithString:@"http://114.80.151.69/oms/video-upload/v/upload/do2.php"];
        ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
        [request setPostValue:[_array objectAtIndex:0] forKey:@"title"];
        [request setPostValue:[_array objectAtIndex:1] forKey:@"memo"];
        [request setPostValue:[_array objectAtIndex:2] forKey:@"tags"];
        [request setPostValue:[_array objectAtIndex:3] forKey:@"fileName"];
        [request setDelegate:obj];
        request.didFinishSelector = @selector(requestFinishedHandler:);
        request.didFailSelector = @selector(requestFailedHandler:);
        [request startAsynchronous];
    }
    NSLog(@"uploadVideoWithData_end");
    return 0;
}

-(int)uploadVideoDataWithVideoURL:(NSURL *)_videoURL withMetaData:(NSMutableArray *)_array{
    //   -1:error   0:network error   1:success
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    if (netStatus!=kNotReachable) { // ok
        reachable = YES;
    }
    if(reachable){
        //weburl=[weburl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSURL *url=[NSURL URLWithString:@"http://114.80.151.69/oms/video-upload/e/enews/upload3.php"];
        ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
        [request setFile:[_videoURL path] forKey:@"Filedata"];
        [request startSynchronous];
        NSError *error = [request error];
        if (!error) {
            NSString *response = [request responseString];
            NSLog(@"no error and responsestring:%@",response);
            url=[NSURL URLWithString:@"http://114.80.151.69/oms/video-upload/v/upload/do2.php"];
            request=[ASIFormDataRequest requestWithURL:url];
            [request setPostValue:response forKey:@"fileName"];
            [request setPostValue:[_array objectAtIndex:0] forKey:@"title"];
            [request setPostValue:[_array objectAtIndex:1] forKey:@"memo"];
            [request setPostValue:[_array objectAtIndex:2] forKey:@"tags"];
            [request startSynchronous];
            NSError *error = [request error];
            if (!error) {
                NSString *response2 = [request responseString];
                NSLog(@"no error and22 responsestring:%@",response2);
                if([response2 isEqualToString:@"1"]){
                    return 1;   //成功 服务器端
                }else{
                    return -1;    //失败 服务器端
                }
            }else{
                return -1;   //失败 客户端或者响应超时
            }
        }else{
            NSLog(@"error in uploading!%@",error);
            return -1;  //失败 客户端
            //[request release];
        }
        NSLog(@"after startSynchronous!");
    }else{
        return 0;   //失败 网络问题
        NSLog(@"network is unavailable!");
    }
}

@end
